/*
 * Copyright 2018-10-21 8:24:50 The JA-SIG Collaborative. All rights reserved.
 * distributed with thi file and available online at
 */
package com.lap.scp.erp.core.impl;

import java.util.Collections;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lap.scd.commons.core.exception.ManagerException;
import com.lap.scd.commons.page.Page;
import com.lap.scp.admin.support.MybatisBatch;
import com.lap.scp.erp.core.AttendanceManager;
import com.lap.scp.erp.dal.AttendanceDAO;
import com.lap.scp.erp.dal.PaymentDAO;
import com.lap.scp.erp.domain.AttendanceDO;
import com.lap.scp.erp.domain.PaymentDO;
import com.lap.scp.erp.query.AttendanceQueryVO;

/**
 * <pre>
 * 出勤记录业务代码默认实现类，这里提供最小元素原子操作，所以尽量少在该类中做流程代码。
 * 业务流程代码应该写在AO中。
 * </pre>
 * 
 * @author 劳水生 Exp
 * @Date 2018-10-21 8:24:50
 * @since 2.0.0
 */
@Service("attendanceManager")
public class DefaultAttendanceManager implements AttendanceManager {
	/**
	 * 出勤记录数据操作对象
	 */
	@Resource
	private AttendanceDAO attendanceDAO;

	@Resource
	private PaymentDAO paymentDAO;

	@Resource
	private MybatisBatch mybatisBatch;

	@Override
	public void insertBatch(List<AttendanceDO> attendList, List<PaymentDO> list) throws ManagerException {
		mybatisBatch.insertBatch("com.lap.scp.erp.dal.PaymentDAO.insertPayment", list);
		for (AttendanceDO att : attendList) {

			for (PaymentDO pay : list) {
				if (pay.getSign().equals(att.getSign())) {
					att.setPaymentId(pay.getId());
					break;
				}
			}
		}
		mybatisBatch.insertBatch("com.lap.scp.erp.dal.AttendanceDAO.insertAttendance", attendList);
	}

	@Override
	public int deleteById(Integer attendanceId) throws ManagerException {
		return attendanceDAO.deleteById(attendanceId);
	}

	@Override
	public AttendanceDO queryById(Integer attendanceId) throws ManagerException {
		return attendanceDAO.queryById(attendanceId);
	}

	@Override
	public List<AttendanceDO> queryByEmployeeIdAndDate(Integer employeeId, List<Date> workDateList)
			throws ManagerException {
		return attendanceDAO.queryByEmployeeIdAndDate(employeeId, workDateList);
	}

	@Override
	public List<AttendanceDO> queryPage(Page page, AttendanceQueryVO queryVO) throws ManagerException {
		int count = attendanceDAO.countPage(queryVO);
		if (count == 0) {
			return Collections.emptyList();
		} else {
			page.setTotalItem(count);
			return attendanceDAO.queryPage(page, queryVO);
		}
	}

	@Override
	public int updateAttendance(AttendanceDO attendanceDO) throws ManagerException {
		int flag = attendanceDAO.updateAttendance(attendanceDO);
		if (flag > 0) {
			PaymentDO paymentDO = attendanceDO.getPaymentDO();
			paymentDAO.updateAmount(paymentDO);
		}
		return flag;
	}

}
